package com.sx.gongzhong.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.sx.gongzhong.BaseActivity;
import com.sx.gongzhong.R;
import com.sx.gongzhong.model.UserInfoBean;
import com.sx.gongzhong.pest.UpdatePasswordPest;
import com.sx.gongzhong.utils.BaseUtil;
import com.sx.gongzhong.utils.ToastUtils;
import com.sx.gongzhong.view.UpdatePasswordView;
import com.sx.gongzhong.widget.LoadDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xiaofei.library.datastorage.DataStorageFactory;
import xiaofei.library.datastorage.IDataStorage;

/**
 * 修改密码
 * Created by admin on 2018/1/2.
 */

public class UpdatePasswordActivity extends BaseActivity implements UpdatePasswordView {
    @BindView(R.id.tv_old_password)
    EditText tvOldPassword;
    @BindView(R.id.et_new_password)
    EditText etNewPassword;
    @BindView(R.id.et_new_confirm_password)
    EditText etNewConfirmPassword;

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_update_password);
        ButterKnife.bind(this);
        setTitleName("修改密码");
    }

    private String password;
    @OnClick({R.id.confirm, R.id.tv_forgot_password})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.confirm:
                BaseUtil.hintKb(this);
                String oldp =  tvOldPassword.getText().toString().trim();
                if ( oldp.length() < 6 || oldp.length() > 20 ){
                    ToastUtils.showToast(UpdatePasswordActivity.this,"密码长度为6-20位");
                    return;
                }
                password =  etNewPassword.getText().toString().trim();
                if ( password.length() < 6 || password.length() > 20 ){
                    ToastUtils.showToast(UpdatePasswordActivity.this,"密码长度为6-20位");
                    return;
                }
                String ncp =  etNewConfirmPassword.getText().toString().trim();
                if ( ncp.length() < 6 || ncp.length() > 20 ){
                    ToastUtils.showToast(UpdatePasswordActivity.this,"密码长度为6-20位");
                    return;
                }
                if (!ncp.equals(password) ){
                    ToastUtils.showToast(UpdatePasswordActivity.this,"两次密码不一样");
                    return;
                }
                LoadDialog.show(UpdatePasswordActivity.this,"修改中...");
                new UpdatePasswordPest(UpdatePasswordActivity.this).updatePassword(this,oldp,password,ncp);
                break;
            case R.id.tv_forgot_password:
                startActivity(new Intent(this,ForgetPwdActivity.class));
                break;
        }
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(UpdatePasswordActivity.this);
        ToastUtils.showToast(UpdatePasswordActivity.this,message);
    }

    @Override
    public void updatePasswordSuccess(Boolean result) {
        LoadDialog.dismiss(UpdatePasswordActivity.this);
        IDataStorage dataStorage = DataStorageFactory.getInstance(getApplicationContext(),
                DataStorageFactory.TYPE_DATABASE);
        if (dataStorage.contains(UserInfoBean.class,getPackageName())){
            UserInfoBean userBean = dataStorage.load(UserInfoBean.class,getPackageName());
            userBean.password = password;
            dataStorage.storeOrUpdate(userBean,getPackageName());
        }
        ToastUtils.showToast(UpdatePasswordActivity.this,"修改成功");
        finish();
    }
}
